<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handlePrint({})" v-auth="'bas:mes:oper:basMesOper:edit'">
          <Icon icon="ant-design:printer-outlined" /> {{ t('工序打印') }}
        </a-button>
      </template>
      <!-- 勾选 -->
      <template #checkBox="{ record, column }">
        <a-checkbox :checked="record[column.dataIndex] == '1' ? true : false" />
      </template>
    </BasicTable>
    <PrintModal @register="registerPrintModal" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasMesOperBasMesOperList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { basMesOperListData } from '/@/api/bas/mes/oper/basMesOper';
  import { basWorkShopTreeData } from '/@/api/bas/mes/workshop/basMesWorkShop';
  import { FormProps } from '/@/components/Form';
  import PrintModal from '/@/components/print/printModal.vue';
  import { useModal } from '/@/components/Modal';
  const { t } = useI18n('bas.mes.oper.basMesOper');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('工序档案管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('工序编码'),
        field: 'code',
        component: 'Input',
      },
      {
        label: t('工序名称'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('归属车间'),
        field: 'workShopCode',
        component: 'TreeSelect',
        componentProps: {
          api: basWorkShopTreeData,
          allowClear: true,
          canSelectParent: false,
        },
      },
      {
        label: t('计价方式'),
        field: 'costType',
        component: 'Select',
        componentProps: {
          dictType: 'mes_price_mode',
        },
      },
      {
        label: t('技能标准'),
        field: 'techLevel',
        component: 'Select',
        componentProps: {
          dictType: 'mes_tech_level',
        },
      },
      {
        label: t('技能控制'),
        field: 'techCtrl',
        component: 'Select',
        componentProps: {
          dictType: 'mes_tech_ctrl',
        },
      },
      {
        label: t('报工方式'),
        field: 'reportType',
        component: 'Select',
        componentProps: {
          dictType: 'mes_report_type',
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_search_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('关键工序'),
        field: 'bkey',
        component: 'Checkbox',
        componentProps: ({ formModel }) => {
          return {
            options: [{ label: '', value: 1 }],
            onChange: (v) => {
              console.log(v.target);
              formModel['bkey'] = v.target.checked ? 1 : 0;
              handleSuccess();
            },
          };
        },
      },
      {
        label: t('是否委外'),
        field: 'bout',
        component: 'Checkbox',
        componentProps: ({ formModel }) => {
          return {
            options: [{ label: '', value: 1 }],
            onChange: (v) => {
              console.log(v.target);
              formModel['bout'] = v.target.checked ? 1 : 0;
              handleSuccess();
            },
          };
        },
      },
      {
        label: t('是否报工'),
        field: 'breport',
        component: 'Checkbox',
        componentProps: ({ formModel }) => {
          return {
            options: [{ label: '', value: 1 }],
            onChange: (v) => {
              console.log(v.target);
              formModel['breport'] = v.target.checked ? 1 : 0;
              handleSuccess();
            },
          };
        },
      },
      {
        label: t('是否检验'),
        field: 'bcheck',
        component: 'Checkbox',
        componentProps: ({ formModel }) => {
          return {
            options: [{ label: '', value: 1 }],
            onChange: (v) => {
              console.log(v.target);
              formModel['bcheck'] = v.target.checked ? 1 : 0;
              handleSuccess();
            },
          };
        },
      },
      {
        label: t('是否上机'),
        field: 'bmachine',
        component: 'Checkbox',
        componentProps: ({ formModel }) => {
          return {
            options: [{ label: '', value: 1 }],
            onChange: (v) => {
              console.log(v.target);
              formModel['bmachine'] = v.target.checked ? 1 : 0;
              handleSuccess();
            },
          };
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('工序编码'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 120,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('工序名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('归属车间'),
      //dataIndex: 'workShopCode',
      dataIndex: 'workShop.name',
      key: 'a.work_shop_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('计价方式'),
      dataIndex: 'costType',
      key: 'a.cost_type',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'mes_price_mode',
    },
    {
      title: t('技能标准'),
      dataIndex: 'techLevel',
      key: 'a.tech_level',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'mes_tech_level',
    },
    {
      title: t('技能控制'),
      dataIndex: 'techCtrl',
      key: 'a.tech_ctrl',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'mes_tech_ctrl',
    },
    {
      title: t('报工方式'),
      dataIndex: 'reportType',
      key: 'a.report_type',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'mes_report_type',
    },
    {
      title: t('关键工序'),
      dataIndex: 'bkey',
      key: 'a.bkey',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'sys_yes_no',
      slot: 'checkBox',
    },
    {
      title: t('是否委外'),
      dataIndex: 'bout',
      key: 'a.bout',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'sys_yes_no',
      slot: 'checkBox',
    },
    {
      title: t('是否报工'),
      dataIndex: 'breport',
      key: 'a.breport',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'sys_yes_no',
      slot: 'checkBox',
    },
    {
      title: t('是否检验'),
      dataIndex: 'bcheck',
      key: 'a.bcheck',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'sys_yes_no',
      slot: 'checkBox',
    },
    {
      title: t('是否上机'),
      dataIndex: 'bmachine',
      key: 'a.bmachine',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'sys_yes_no',
      slot: 'checkBox',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'sys_search_status',
    },
    {
      title: t('更新日期'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('备注说明'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const [registerPrintModal, { openModal: openPrintModal }] = useModal();
  const [registerTable, { reload, getSelectRows }] = useTable({
    api: basMesOperListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleSuccess() {
    reload();
  }
  async function handlePrint() {
    let arr = await getSelectRows();
    if (getSelectRows().length == 0) {
      showMessage(t('请先选择一行数据'));
      return;
    }
    const idsArr = arr.map((item) => {
      return item.id;
    });
    let params = {
      ids: idsArr.join(','),
      title: '工序打印',
      height: '0.9',
      width: '0.9',
      fileName: '工序打印',
    };
    openPrintModal(true, params);
  }
</script>
